import random
import pandas as pd
from datetime import datetime, timedelta

# 设置随机种子，保证结果可复现
random.seed(42)

# 定义生成函数
def generate_graph_data(num_edges=100000, num_nodes=2000):
    # 准备存储数据的列表
    data = []
    
    # 可用的颜色列表（从CSV文件观察得到）
    colors = ["#E8C960", "#5779A3", "#6B9E59", "#84B5B2", "#F2A2A8", "#D1605E", "#A77D9F", "#E59244",
              "#4287f5", "#42f5a7", "#f542e0", "#f54242", "#f5a142", "#c8f542", "#426af5", "#9e42f5", 
              "#f5428e", "#42f5d9", "#8ef542", "#f5d142", "#4255f5", "#f54268", "#42f59e", "#f5d942"]
    
    # 定义日期范围（2019年1月至2024年12月）
    start_date = datetime(2019, 1, 1)
    end_date = datetime(2024, 12, 31)
    
    # 星期几的中文表示
    weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    
    # 月份的英文表示
    months = ["January", "February", "March", "April", "May", "June", 
              "July", "August", "September", "October", "November", "December"]
    
    # 确保所有节点都被使用
    used_nodes = set()
    
    # 首先确保每个节点都至少出现一次（作为源节点）
    for node in range(1, num_nodes + 1):
        if len(data) >= num_edges:
            break
            
        source = str(node)
        used_nodes.add(source)
        
        # 随机选择一个目标节点（不同于源节点）
        target = str(random.randint(1, num_nodes))
        while target == source:
            target = str(random.randint(1, num_nodes))
        used_nodes.add(target)
        
        # 随机生成值（从1到10000）
        value = str(random.randint(1, 10000))
        
        # 随机选择一种颜色
        color = random.choice(colors)
        
        # 随机生成日期
        random_days = random.randint(0, (end_date - start_date).days)
        random_date = start_date + timedelta(days=random_days)
        weekday = weekdays[random_date.weekday()]
        month = months[random_date.month - 1]
        formatted_date = f"{weekday}, {random_date.day} {month} {random_date.year}"
        
        # 添加到数据列表
        data.append([source, target, value, color, formatted_date])
    
    # 如果所有节点都已使用但边数不足，继续生成剩余的边
    remaining_edges = num_edges - len(data)
    for _ in range(remaining_edges):
        # 随机生成源节点和目标节点
        source = str(random.randint(1, num_nodes))
        
        # 确保目标节点与源节点不同
        target_options = [str(random.randint(1, num_nodes)), str(min(int(source) + random.randint(1, 100), num_nodes))]
        target = random.choice(target_options)
        while target == source:
            target = random.choice(target_options)
        
        # 随机生成值（从1到10000）
        value = str(random.randint(1, 10000))
        
        # 随机选择一种颜色
        color = random.choice(colors)
        
        # 随机生成日期
        random_days = random.randint(0, (end_date - start_date).days)
        random_date = start_date + timedelta(days=random_days)
        weekday = weekdays[random_date.weekday()]
        month = months[random_date.month - 1]
        formatted_date = f"{weekday}, {random_date.day} {month} {random_date.year}"
        
        # 添加到数据列表
        data.append([source, target, value, color, formatted_date])
    
    # 创建DataFrame
    df = pd.DataFrame(data, columns=["source", "target", "value", "color", "date"])
    
    print(f"实际使用的节点数量: {len(set(df['source'].tolist() + df['target'].tolist()))}")
    
    return df

# 生成数据
graph_data = generate_graph_data(10000, 10000)

# 保存为CSV文件，使用分号作为分隔符
graph_data.to_csv("large_graph_data.csv", sep=";", index=False)

print(f"已成功生成包含10000条边和10000个节点的图数据，并保存到large_graph_data.csv")